#pragma once 

#include<iostream>
#include<set>
#include<cstdio>
#include<string>


class CommandExc
{
    public:
    CommandExc()
    {
        _white_list.insert("ls");
        _white_list.insert("pwd");
        _white_list.insert("ls -l");
        _white_list.insert("ll");
        _white_list.insert("touch");
        _white_list.insert("who");
        _white_list.insert("whoami");
    }
    ~CommandExc()
    {
    }
    bool check(std::string& message)
    {
       auto pos = _white_list.find(message);
       return pos != _white_list.end();
       
    }
    std::string Exc(std::string& command)
    {
        if(!check(command))
        {
            std::cout<<command <<"不合法"<<std::endl;
            return std::string("命令不合法");
        }
        
        FILE* pp=::popen(command.c_str(),"r");
        if(pp==nullptr)
        {
            return std::string("popen false");
        }
        char buffer[1024];
        std::string result;
        while(true)
        {
            char* get = ::fgets(buffer,sizeof(buffer),pp);
            if(!get)
            {
                break;
            }
            result+=buffer;
        }
        ::pclose(pp);
        return result;

    }
    private:
    std::set<std::string> _white_list;

};
